

<style>
    .td{
        line-height: normal;
        word-wrap: break-word;
        word-break: break-all;
        margin-top: 5px;
        padding-top: 10px;
    }
</style>

<div class="padding-big">
    <div class="op">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">开始时间：</label>
                <div class="layui-input-inline">
                    <input type="date" name="from" id="from" lay-verify="date" placeholder="yyyy-MM-dd"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline" style="margin-left: 0px">
                <label class="layui-form-label">结束时间：</label>
                <div class="layui-input-inline">
                    <input type="date" name="to" id="to" lay-verify="date" placeholder="yyyy-MM-dd"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-inline">
                    <input type="text" name="username" id="username" maxlength="16" lay-verify="title" autocomplete="off"
                           placeholder="请输入用户名" class="layui-input">
                </div>
            </div>
            <button class="layui-btn layui-btn-normal" id="warnSearch" onclick="getWarningLogsInfoList(1)"><i class="fa fa-search"></i>&nbsp;搜索</button>
        </div>
    </div>
    <div class="offcial-table margin-top clearfix" style="background-color: #fff;">
        <div class="offcial-table tr-border margin-big-top clearfix">
            <div class="tr-th clearfix">
                <div class="th w15">
                    告警模块
                </div>
                <div class="th w15">
                    告警类型
                </div>
                <div class="th w15">
                    告警级别
                </div>
                <div class="th w15">
                    告警IP地址
                </div>

                <div class="th w25">
                    告警详情
                </div>
                <div class="th w15">
                    告警时间
                </div>
            </div>

            <div id="tbody">
            </div>
        </div>
    </div>

    <div class="show-page padding-big-right">
        <div class="page">
            <div class="page">
                <ul class="offcial-page margin-top margin-big-right">
                    <li>
                        <a href="javascript:void(0)" onclick="logsWarningFirstPage()" class="next" >首     页</a>
                    </li>
                    <li>
                        <a href="javascript:void(0)" onclick="logsWarningLastPage()" class="next">上一页</a>
                    </li>
                    <li><span class="fl">第<em class="margin-small-left margin-small-right" id="pageNum">1</em>页</span>
                    </li>
                    <li>
                        <a href="javascript:void(0)" onclick="logsWarningNextPage()" class="next">下一页</a>
                    </li>
                </ul>
            </div>
        </div>
    </div>

</div>
<script>
    checkRole('auditor');
    var num=1;
    /**
     * 查询告警日志记录接口
     * @param pageNum
     * @param pageSize
     * @param callback
     */
    function listWarningLogsByCond(username, from, to, pageNum, pageSize){
        $.ajax({
            url: HOST.concat("/listWarningLogInfo?username="+username
                +"&fromTime="+ from
                + "&toTime="+ to
                +"&pageNum=" + pageNum
                + "&pageSize=" + pageSize
                + "&token=" + config.token) ,
            type: "GET",
            success: function(datas){
                checkLogin(datas);
                console.log(datas)
                addWarningContent(datas);
            },
            error: function(){
                console.log("请求日志列表错误！");
            },
            dataType: "json",
        });
    }

    /**
     * 获取指定页码的告警日志
     * */
    function getWarningLogsInfoList(num){
        var username=$("#username").val();
        if( username.trim()!="" && !checkLogUsername(username)){
            alert("用户名不能超过16位，且不包含除出下划线和减号以外的特殊字符");
            $("#username").val("");
            return;
        }
        var from=$("#from").val();
        var to=$("#to").val();
        if(!isNull(from)&&!isNull(to)) {
            if (compareFromAndTo(from,to)){
                from = from.replace(/-/g, "/") + " 00:00:00";
                to = to.replace(/-/g, "/") + " 23:59:59";
                listWarningLogsByCond(username, from, to, num, pageSize);
            }else {
                alert("时间区间先后存在问题!");
            }
        }else if(!isNull(from)){
            from = from.replace(/-/g, "/") + " 00:00:00";
            listWarningLogsByCond(username, from, to, num, pageSize);
        }else if(!isNull(to)){
            to = to.replace(/-/g, "/") + "23:59:59";
            listWarningLogsByCond(username, from, to, num, pageSize);
        }else {
            listWarningLogsByCond(username, from, to, num, pageSize);
        }
    }

    /**
     * 添加告警日志
     * */
    function addWarningContent(res){
        //查询成功
        console.log(res);
        if(res.meta.state==="success") {
            var list = res.data;
            console.log("有查询记录");
            $("#tbody").empty();
            $("#pageNum").text(num);
            addTd(list);
        }
        //查询失败
        else if(res.meta.state=="failed"){
            if(num==1){
                $("#tbody").empty();
                alert("无查询结果!");
            }else {
                alert("已经是最后一页!");
                num--;
            }
        }
    }

    /**
     * 获取上一页
     * */
    function logsWarningLastPage(){
        if(num<=1){
            num = 1;
            alert("已经是第一页!");
            $("#pageNum").text(num);
        }else{
            num--;
            getWarningLogsInfoList(num);
        }
    }

    /**
     * 获取下一页
     * */
    function logsWarningNextPage(){
        num++;
        getWarningLogsInfoList(num);
    }

    /**
     * 获取首页
     * */
    function logsWarningFirstPage(){
        num = 1;
        $("#pageNum").text(num);
        getWarningLogsInfoList(num);
    }

    /**
     * 添加具体日志内容
     * */
    function addTd (list){
        for (var p in list) {
            var item = list[p];//单条数据
            var createTime = item["createTime"];
            var warningType = item["warningType"];
            var warningModuleName = item["warningModuleName"];
            var warningLevel = item["warningLevel"];
            var ipAddress = item["ipAddress"];
            var warningInfo = item["warningInfo"];

            $("#tbody").append("<div class=\"op-data tr clearfix border-bottom-none\">" +
                "                        <div class=\"td w15\">" + warningModuleName + "</div>" +
                "                        <div class=\"td w15\">" + warningType + "</div>" +
                "                        <div class=\"td w15\">" + warningLevel + "</div>" +
                "                        <div class=\"td w15\">" + ipAddress + "</div>" +
                "                        <div class=\"td w25\" style='text-indent: 2em;'>" + warningInfo + "</div>" +
                "                        <div class=\"td w15\">" + createTime + "</div>" +
                "               </div>");
        }
    }


    //进入页面就直接请求
    getWarningLogsInfoList(1);
</script>


